import _ from 'lodash'

const localStorage = window.localStorage;
const prefix = 'pwc.sp.';
const generateKey = (key: string) => `${prefix}${key}`;

export default {

  set: (key: any, val: any) => {
    if (!key || !val) {
      throw new Error('key or value is undefined');
    }
    var storageData = {};
    key = generateKey(key);
    if (_.isString(val)) {
      localStorage.setItem(key, val);
    } else {
      localStorage.setItem(key, JSON.stringify(val));
    }
  },

  get: (key: any) => {
    if (!key) {
      throw new Error('key or value is undefined');
    }
    key = generateKey(key);
    var data = localStorage.getItem(key);
    try {
      if (data)
        return JSON.parse(data);
      else
        return ""
    }
    catch (e) {
      return data;
    }
  },

  remove: (key: any) => {
    key = generateKey(key);
    localStorage.removeItem(key);
  }

}